
#include"video/MeshManager.h"
#include<assert.h>
namespace KEngine
{


MeshManager::MeshManager():mTextureMgr(0)
{

}

MeshManager::~MeshManager()
{
	ResMap::iterator it = _resMap.begin();
	for(;it!=_resMap.end();++it)
	{
		assert(it->second->getRefCount()==1);
		KSafeRelease(it->second);
	}
	_resMap.clear();
}

Mesh* MeshManager::create(std::string& name)
{
	Mesh* p = getT(name);
	if(p)
	{
		p->addRef();
		return p;
	}
	p = new Mesh();
	if(!p->load(name.c_str(),mTextureMgr))
	{
		KSafeRelease(p);
		return 0;
	}
	_resMap[name] = p;
	return p;
}

void MeshManager::releaseByName(std::string& name)
{
	ResMap::iterator it = _resMap.find(name);
	if(it!=_resMap.end())
	{
		Mesh* p = it->second;
		if(p->getRefCount()==1)
		{
			_resMap.erase(it);
		}
		KSafeRelease(p);
	}
	else
	{
		assert(0);
	}
}


}